package com.bct.jxc.view;
/**
 * 入库查询信息
 */

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Vector;

import javax.swing.JInternalFrame;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JTextField;

import com.bct.jxc.common.CommonFactory;
import com.bct.jxc.common.StringUtil;
import com.bct.jxc.model.InportInfo;
import com.bct.jxc.model.Inport;
import com.bct.jxc.server.InportServer;

import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ImageIcon;
import java.awt.Font;
import javax.swing.JCheckBox;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;

public class InportInfoSearchFrame extends JInternalFrame implements ActionListener {
	private JTextField searchValue;
	private JComboBox comboBox;//查询选项
	JCheckBox chckbxNewCheckBox;//日期查询
	private JTable table;//表格一定要放在JScrollPane，这样数据多的时候可以出现滚动条
	private MyTableModel tableModel;//自定义表格模板，为了适配数据
	private InportServer inportServer;
	private JTextField startTimeTf;
	private JTextField endTimeTf;

	/**
	 * 初始化视窗
	 */
	public InportInfoSearchFrame() {
		//支持放大缩小
		super("入库查询",true,true,true,true);
		//获取屏幕尺寸
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		setBounds(0,0,screenSize.width*2/3,screenSize.height*2/3);
		getContentPane().setLayout(new BorderLayout(0, 0));
		
		JPanel panel = new JPanel();
		getContentPane().add(panel, BorderLayout.NORTH);
		
		JLabel lblNewLabel = new JLabel("请选择查询条件：");
		lblNewLabel.setFont(new Font("楷体", Font.BOLD, 16));
		lblNewLabel.setIcon(new ImageIcon(InportInfoSearchFrame.class.getResource("/com/bct/jxc/images/customer_query.png")));
		
		comboBox = new JComboBox();
		comboBox.setFont(new Font("楷体", Font.BOLD, 15));
		comboBox.setModel(new DefaultComboBoxModel(new String[] {"入库单号", "供应商名称", "商品名称", "经手人"}));
		
		searchValue = new JTextField();
		searchValue.setColumns(25);
		
		JButton searchOne = new JButton("查询");
		searchOne.setIcon(new ImageIcon(InportInfoSearchFrame.class.getResource("/com/bct/jxc/images/login.png")));
		searchOne.setFont(new Font("楷体", Font.BOLD, 16));
		//给查询绑定事件
		searchOne.addActionListener(this);
		
		JButton searchList = new JButton("显示全部信息");
		searchList.setIcon(new ImageIcon(InportInfoSearchFrame.class.getResource("/com/bct/jxc/images/provider_query.png")));
		searchList.setFont(new Font("楷体", Font.BOLD, 16));
		//给显示全部信息绑定事件
		searchList.addActionListener(this);
		
		JPanel panel_1 = new JPanel();
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblNewLabel)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(comboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addComponent(searchValue, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addComponent(searchOne)
					.addGap(18)
					.addComponent(searchList)
					.addContainerGap(159, Short.MAX_VALUE))
				.addComponent(panel_1, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 894, Short.MAX_VALUE)
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGap(8)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel)
						.addComponent(comboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(searchValue, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(searchOne)
						.addComponent(searchList))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 40, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(18, Short.MAX_VALUE))
		);
		
		chckbxNewCheckBox = new JCheckBox("指定日期查询：");
		chckbxNewCheckBox.setFont(new Font("楷体", Font.BOLD, 16));
		panel_1.add(chckbxNewCheckBox);
		
		JLabel lblNewLabel_1 = new JLabel("从");
		lblNewLabel_1.setFont(new Font("楷体", Font.BOLD, 16));
		panel_1.add(lblNewLabel_1);
		
		startTimeTf = new JTextField();
		panel_1.add(startTimeTf);
		startTimeTf.setColumns(20);
		
		JLabel lblNewLabel_2 = new JLabel("到");
		lblNewLabel_2.setFont(new Font("楷体", Font.BOLD, 16));
		panel_1.add(lblNewLabel_2);
		
		endTimeTf = new JTextField();
		panel_1.add(endTimeTf);
		endTimeTf.setColumns(20);
		panel.setLayout(gl_panel);
		//创建表格的列（标题栏文字）
		String[] columNames = {"入库单号","商品数量","合计价格","验收结论","供应商名称","入库时间","操作者"
				,"经手人","支付方式","商品名称"};
		//实例化模板
		tableModel = new MyTableModel<InportInfo>(columNames, InportInfo.class);
		//创建表格面板
		table = new JTable(tableModel);
		table.setFont(new Font("宋体", Font.BOLD, 14));
		//设置表格大小
		table.setPreferredScrollableViewportSize(new Dimension(screenSize.width*2/3-80, screenSize.height*2/3));
		//视图高度填充
		table.setFillsViewportHeight(true);
		table.setAutoCreateRowSorter(true);
		//将表格放到滑动面板中
		JScrollPane scrollPane = new JScrollPane(table);
		getContentPane().add(scrollPane, BorderLayout.CENTER);
		//实例化server
		inportServer = CommonFactory.getInportServer();
	}
	
	/**
	 * 点击事件
	 * @param e
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		String taget = e.getActionCommand();//获取按钮信息
		switch (taget) {
		case "查询":
			//封装数据
			InportInfo inportInfo = new InportInfo();
			if(chckbxNewCheckBox.isSelected()) {//根据指定时间查询
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				String startTime = startTimeTf.getText().trim();
				String endTime = endTimeTf.getText().trim();
				if(StringUtil.isEmpty(startTime) || StringUtil.isEmpty(endTime)) {
					JOptionPane.showMessageDialog(null, "开始时间或结束时间不能为空！","警告",JOptionPane.WARNING_MESSAGE);
					return;
				}
				if(!StringUtil.validateDate(startTime)) {
					JOptionPane.showMessageDialog(null, "输入的开始时间格式不正确！","警告",JOptionPane.WARNING_MESSAGE);
					return;
				}
				if(!StringUtil.validateDate(endTime)) {
					JOptionPane.showMessageDialog(null, "输入的结束时间格式不正确！","警告",JOptionPane.WARNING_MESSAGE);
					return;
				}
				try {
					inportInfo.setStartTime(sdf.parse(startTime));
					inportInfo.setEndTime(sdf.parse(endTime));
				} catch (ParseException e1) {
					e1.printStackTrace();
				}
			}else {
				//搜索框非空验证
				String searchVal = searchValue.getText().trim();
				if(StringUtil.isEmpty(searchVal)) {
					JOptionPane.showMessageDialog(null, "请输入搜索的值","警告",JOptionPane.WARNING_MESSAGE);
					return;
				}
				String type = comboBox.getSelectedItem().toString();//获取查询类型
				switch (type) {
				case "入库单号":
					inportInfo.setId(searchVal);
					break;
				case "供应商名称":
					inportInfo.setProviderName(searchVal);
					break;
				case "商品名称":
					inportInfo.setProductName(searchVal);
					break;
				case "经手人":
					inportInfo.setSponser(searchVal);
					break;
				default:
					break;
				}
			}
			updateAllDate(inportInfo);//更新数据
			break;
		case "显示全部信息":
			updateAllDate(null);//更新数据
			break;
		default:
			break;
		}
		
	}
	/**
	 * 更新数据
	 * @param Inport
	 */
	private void updateAllDate(InportInfo info) {
		//查询数据库
		List<InportInfo> infos = inportServer.findAll(info);
		if(infos==null || infos.size()==0) {
			JOptionPane.showMessageDialog(null, "没有满足你条件的入库单记录","警告",JOptionPane.WARNING_MESSAGE);
			//清空输入框中的值
			searchValue.setText("");
			return;
		}
		if(infos != null) {
			Vector<InportInfo> list = new Vector<InportInfo>();
			for (InportInfo c : infos) {
				list.add(c);
			}
			//更新数据
			tableModel.updateData(list);
			//清空输入框中的值
			searchValue.setText("");
		}
	}
}
